package com.tedu.mybatis.mapper;

import com.tedu.mybatis.pojo.User;
import org.springframework.stereotype.Component;

import java.sql.*;


public class UserMapperImpl implements UserMapper {
    @Override
    public int insert(User user) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/weibo?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root",
                    "root");
            String sql = "INSERT INTO user(username,password,nickname) " +
                         "VALUES(?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
//            ps.setDate(4,user.getCreated());

            return ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
